package server;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class URLPathClassLoader  {//extends URLClassLoader

   /* private String packageName = "web_demo_war_exploded.WEB-INF.classes.com.lagou.edu.servlet";

    public URLPathClassLoader(URL[] classPath, ClassLoader parent){

        super(classPath, parent);
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {

        Class<?> aClass = findLoadedClass(name);
        if(aClass != null){

            return aClass;
        }
        if(!packageName.startsWith(name)){

            return super.loadClass(name);
        }
        return null;
    }*/

    public static void main(String[] args) throws Exception {
//        file://Users/apple/temp"
        /*URL[] urls = {new URL("file://D:/webapps")};

        URLPathClassLoader urlPathClassLoader = new URLPathClassLoader(urls, getSystemClassLoader());
        Class say = urlPathClassLoader.findClass("web_demo_war_exploded.WEB-INF.classes.com.lagou.edu.servlet.TestServlet");
        Method method = say.getMethod("doGet");
        Object instance = say.newInstance();
        method.invoke(instance);

        HttpServlet httpServlet = (HttpServlet)instance;
        httpServlet.doGet(null,null);*/



            File filpath=new File("");
//            String path = "file:"+filpath.getAbsoluteFile()+"\\src\\datas\\";

            URL newurl=new URL("file:D:/webapps/web_demo_war_exploded/WEB-INF/classes");
            URLClassLoader classLoader=new URLClassLoader(new URL[]{newurl});
            Class<?> methtClass = classLoader.loadClass("com.lagou.edu.servlet.TestServlet");
            Object obj = methtClass.newInstance();
            methtClass.getDeclaredMethod("doGet").invoke(obj);

            System.out.println(classLoader);



     /*   File file = new File("");
//        "file:D\\webapps\\web_demo_war_exploded\\WEB-INF\\classes\\com\\lagou\\edu\\servlet"
        URL url = file.toURL();
        URLClassLoader loader = new URLClassLoader(new URL[]{url});
        Class aClass = loader.loadClass("webapps\\web_demo_war_exploded\\WEB-INF\\classes\\com\\lagou\\edu\\servlet\\TestServlet.class");
        //利用Java反射机制创建实例测试方法
        HttpServlet httpServlet = (HttpServlet)aClass.newInstance();
        httpServlet.doGet(null,null);*/




    }


}
